package main;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

public class MatrixGeneration extends BaseGeneration {

	public static final String TERM_COUNT_FILE_NAME = "data/term-count-of-txts";

	public static final String OUTPUT_FILE_NAME = "data/matrix-generation-txts";

	public void gen() {
		try {
			readTermCount(TERM_COUNT_FILE_NAME);
			writeTermsCount();
			// genData();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void writeTermsCount() {
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(OUTPUT_FILE_NAME));
			Set<Entry<String, Topic>> set = topics.entrySet();
			List<Entry<String, Topic>> list = new ArrayList<Entry<String, Topic>>(set);
			Collections.sort(list, new comEntry());
			for (Iterator<Entry<String, Topic>> iter = list.iterator(); iter.hasNext();) {
				Topic topic = iter.next().getValue();
				// bw.write("Topic #" + topic.getId() + " full terms:" +
				// StringUtils.join(topic.getFullTerms(), ",") + "\r\n");
				for (Iterator<Passage> itp = topic.getPassages().iterator(); itp.hasNext();) {
					Passage psg = itp.next();
					bw.write(psg.getTopicID() + "," + psg.getPassageID() + "," + psg.getTermsCount() + ","
							+ psg.getTotalTermsCount() + "," + psg.getTerms() + "\r\n");
				}
			}
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		MatrixGeneration mg = new MatrixGeneration();
		mg.gen();
	}
}
